package palmtop.com.palmtop.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import net.grandcentrix.tray.AppPreferences;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.activity.AddClueActivity;
import palmtop.com.palmtop.activity.AddPopulationActivity;
import palmtop.com.palmtop.activity.ClueAcountActivity;
import palmtop.com.palmtop.activity.ClueActivity;
import palmtop.com.palmtop.activity.PopulationActivity;
import palmtop.com.palmtop.activity.PopulationacountActivity;
import palmtop.com.palmtop.model.Clue;
import palmtop.com.palmtop.model.Reportpopulation;

public class ReportFragment extends Fragment {


    @BindView(R.id.populationBtn)
    ImageButton populationBtn;
    @BindView(R.id.populationLayout)
    LinearLayout populationLayout;
    @BindView(R.id.clueBtn)
    ImageButton clueBtn;
    @BindView(R.id.cluelistLayout)
    LinearLayout cluelistLayout;
    @BindView(R.id.addpopulationBtn)
    ImageButton addpopulationBtn;
    @BindView(R.id.addpopulationLayout)
    LinearLayout addpopulationLayout;
    @BindView(R.id.addclueBtn)
    ImageButton addclueBtn;
    @BindView(R.id.addclueLayout)
    LinearLayout addclueLayout;
    @BindView(R.id.mygrids)
    GridLayout mygrids;
//    private OnFragmentInteractionListener mListener;
    private Unbinder unbinder;
    String role_id;
    String ssgrid;

    public ReportFragment() {
        // Required empty public constructor
    }


    public static ReportFragment newInstance(String param1, String param2) {
        ReportFragment fragment = new ReportFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_report, null);
        unbinder = ButterKnife.bind(this, view);
        init(view);
        return view;
    }

    private void init(View view) {
        AppPreferences appPreferences = new AppPreferences(getActivity());
        //permission = appPreferences.getString("permission","");
        String username = appPreferences.getString("loginname","");
        String userid = appPreferences.getString("userid","");
        role_id = appPreferences.getString("role_id","");
        ssgrid = appPreferences.getString("ssgrid","");
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            addpopulationLayout.setVisibility(View.GONE);
            addclueLayout.setVisibility(View.GONE);
            mygrids.removeView(addpopulationLayout);
            mygrids.removeView(addclueLayout);

        }else if(role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")){
            addpopulationLayout.setVisibility(View.GONE);
            addclueLayout.setVisibility(View.GONE);
            mygrids.removeView(addpopulationLayout);
            mygrids.removeView(addclueLayout);

        }else{
            populationLayout.setVisibility(View.GONE);
            cluelistLayout.setVisibility(View.GONE);
            mygrids.removeView(populationLayout);
            mygrids.removeView(cluelistLayout);
        }

    }

    /*// TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }*/

   /* @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }*/

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.populationBtn)
    public void onPopulationBtnClicked() {
        Intent i = new Intent();
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            i.setClass(getActivity(), PopulationacountActivity.class);
        }else{
            Reportpopulation reportpopulation = new Reportpopulation();
            reportpopulation.setUnitid(Integer.valueOf(ssgrid));
            i.putExtra("reportpopulation",reportpopulation);
            i.setClass(getActivity(), PopulationActivity.class);
        }

        startActivity(i);
    }

    @OnClick(R.id.clueBtn)
    public void onClueBtnClicked() {
        Intent i = new Intent();

        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            i.setClass(getActivity(), ClueAcountActivity.class);
        }else{
            Clue clue = new Clue();
            clue.setUnitid(ssgrid);
            i.putExtra("clue",clue);
            i.setClass(getActivity(), ClueActivity.class);
        }
        startActivity(i);
    }

    @OnClick(R.id.addpopulationBtn)
    public void onAddpopulationBtnClicked() {
        Intent i = new Intent();
        i.setClass(getActivity(), AddPopulationActivity.class);
        startActivity(i);
    }

    @OnClick(R.id.addclueBtn)
    public void onAddclueBtnClicked() {
        Intent i = new Intent();
        i.setClass(getActivity(), AddClueActivity.class);
        startActivity(i);
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
